<template>
	<el-container>
		<el-header>
			<div class="left-panel" ref="leftp">
				<el-button type="primary" icon="el-icon-plus" @click="add"></el-button>
				<el-button type="primary" icon="el-icon-plus" @click="addPage">页面新增</el-button>
				<el-button type="primary" @click="printt">打印</el-button>
			</div>
		</el-header>
		<el-main class="nopadding">
			<scTable ref="table" :apiObj="list.apiObj" row-key="id" @selection-change="selectionChange" stripe>
				<el-table-column type="expand">
					<template #default="scope">
						<el-table :ref="getRef(scope.row)" :data="scope.row.detail.data" border>
							<el-table-column label="货号" prop="goods_number" width="100"></el-table-column>
							<el-table-column label="品名" prop="goods_desc" width="180"></el-table-column>
							<el-table-column label="颜色" prop="color" width="180"></el-table-column>
							<el-table-column v-for="item in scope.row.detail.thead" :key="item" :label="item" :prop="item" width="180"></el-table-column>
						</el-table>
						<el-button type="primary" @click="print(scope.row)">打印</el-button>
					</template>
				</el-table-column>
				<el-table-column label="类型" prop="name" width="90">
					<template #default="scope">
						<el-tag  style="font-weight: bold; font-size: 18px" v-if="scope.row.type == 1">入库</el-tag>
						<el-tag  style="font-weight: bold; font-size: 18px" type="success" v-if="scope.row.type == 2">发货</el-tag>
					</template>
				</el-table-column>
				<el-table-column label="客户" prop="company_name" width="100"></el-table-column>
				<el-table-column label="时间" prop="created_at" width="150"></el-table-column>
				
				<el-table-column label="库存量" prop="stock_sum" width="150"></el-table-column>
				<el-table-column label="操作" fixed="right" align="right" width="300">
					<template #default="scope">
						<el-button type="primary" size="small	" @click="print(scope.row)">打印</el-button>
						<el-button type="primary" plain size="small" @click="table_edit(scope.row)">编辑</el-button>
						<el-button type="primary" plain size="small" @click="table_edit_page(scope.row)">页面编辑</el-button>
						<el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
							<template #reference>
								<el-button plain type="danger" size="small">删除</el-button>
							</template>
						</el-popconfirm>
					</template>
				</el-table-column>
			</scTable>
		</el-main>
	</el-container>

	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSaveSuccess" @closed="dialog.save=false"></save-dialog>

	<print-dialog v-if="dialog.print" ref="printDialog" @success="printhandleSaveSuccess" @closed="dialog.print=false"></print-dialog>

	<el-drawer v-model="dialog.info" :size="800" title="详细" direction="rtl" destroy-on-close>
		<info ref="infoDialog"></info>
	</el-drawer>

</template>

<script>
	import saveDialog from './save'
	import printDialog from './print'
	import info from './info'
	// import print from '@/utils/print'

	export default {
		name: 'listCrud',
		components: {
			saveDialog,
			printDialog,
			info
		},
		data() {
			return {
				dialog:{
					save: false,
					info: false,
					print: false,
				},
				list: {
					apiObj: this.$API.finance.list
				},
				selection: [],
				searchOption: {
					type: "",
					is_balance: "",
					pageCount: "",
					created_at: "",
				},
			}
		},
		mounted() {

		},
		methods: {
			upsearch(page = 1) {
				this.$refs.table.upData(this.searchOption, page)		
			},
			getRef(row) {
				return "finance"+ row.id
			},
			print(row) {
				this.dialog.print = true
				this.$nextTick(() => {
					this.$refs.printDialog.open('show').setData(row)
				})
			},
			
			//窗口新增
			add(){
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open()
				})
			},
			//窗口编辑
			table_edit(row){
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open('edit').setData(row)
				})
			},
			//页面新增
			addPage(){
				this.$router.push({
					path: '/template/list/crud/detail',
				})
			},
			//页面编辑
			table_edit_page(row){
				this.$router.push({
					path: '/template/list/crud/detail',
					query: {
						id: row.id
					}
				})
			},
			//查看
			table_show(row){
				this.dialog.info = true
				this.$nextTick(() => {
					this.$refs.infoDialog.setData(row)
				})
			},
			//删除明细
			async table_del(row, index){
				var reqData = {id: row.id}
				var res = await this.$API.demo.post.post(reqData);
				if(res.code == 200){
					this.$refs.table.removeIndex(index)
					this.$message.success("删除成功")
				}else{
					this.$alert(res.message, "提示", {type: 'error'})
				}
			},
			//批量删除
			async batch_del(){
				var confirmRes = await this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
					type: 'warning',
					confirmButtonText: '删除',
					confirmButtonClass: 'el-button--danger'
				}).catch(() => {})

				if(!confirmRes){
					return false
				}

				var ids = this.selection.map(v => v.id)
				this.$refs.table.removeKeys(ids)
				this.$message.success("操作成功")

			},
			//表格选择后回调事件
			selectionChange(selection){
				this.selection = selection
			},
			//本地更新数据
			handleSaveSuccess(data, mode){
				//为了减少网络请求，直接变更表格内存数据
				if(mode=='add'){
					this.$refs.table.unshiftRow(data)
				}else if(mode=='edit'){
					this.$refs.table.updateKey(data)
				}

				//当然也可以暴力的直接刷新表格
				// this.$refs.table.refresh()
			}
		}
	}
</script>

<style>
</style>
